var reg1 = /0*([1-9]\d*|0\.\d+)/;
var reg2 = /(?:\.0*|(\.\d+?)0+)$/;
function showLoading(content){
	uni.showLoading({
		title : content,
		mask:true
	});
}
function hideLoading(){
	uni.hideLoading();
}
function showToastSuc(content){
	uni.showToast({
		title: content,
		icon: 'success',
		duration: 1800
	})
}
function showToast(content,duration){
	uni.showToast({
		title: content,
		icon: 'none',
		duration: duration == undefined ? 1800 : duration
	})
}
function setClipborarData(content){
	uni.setClipboardData({
		data:content,
		success:()=>{
			uni.showToast({
				title: '复制成功',
				icon: 'success',
				duration: 1800
			})
		}
	}) 
}
function navigateTo(page){
	uni.navigateTo({
		url: page
	})
}
function comNavigateToLogin(opt,canRefreshFlag){
	uni.navigateTo({
		url:'/pages/login/login?loadOpt=' + opt + '&canRefreshFlag=' + canRefreshFlag
	})
}
function redirectTo(page){
	uni.redirectTo({
		url: page
	})
}
function trim(str){
	return str.replace(/(^\s*)|(\s*$)/g, ""); 
}
function patternNum(str){
	if(str != ''){
		str = parseFloat(str.replace(reg1, '$1').replace(reg2, '$1')).toFixed(2);
	}else{
		str = 0;
	}
	return str;
}
function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
function openMapNaviagtion(lon,lat,cpyName){
	//打开第三方地图
	var url = "",mapApp = '';
	if (plus.os.name == "Android") {//判断是安卓端
		plus.nativeUI.actionSheet({//选择菜单
			title: "",
			cancel: "取消",
			buttons: [{title: "高德地图"},{title: "百度地图"},{title: "腾讯地图"}]
		}, function(e) {
			switch (e.index) {
				//下面是拼接url,不同系统以及不同地图都有不同的拼接字段
				case 1:
					mapApp = '高德地图';
					url = `androidamap://viewMap?sourceApplication=appname&poiname=${cpyName}&lat=${lat}&lon=${lon}&dev=0`;
					break;
				case 2:
					mapApp = '百度地图';
					url = `baidumap://map/marker?location=${lat},${lon}&title=${cpyName}&coord_type=gcj02&src=andr.baidu.openAPIdemo`;
					break;
				case 3:
					//注意referer=xxx的xxx替换成你在腾讯地图开发平台申请的key
					mapApp = '腾讯地图';
					url = `qqmap://map/geocoder?coord=${lat},${lon}&referer=xxx`;
					break;
				default:
					break;
			}
			if (url != "") {
				url = encodeURI(url);
				//plus.runtime.openURL(url,function(e){})调起手机APP应用
				plus.runtime.openURL(url, function(e) {
					uni.showToast({
						icon:'none',
						title:'本机未安装' + mapApp + '应用'
					})
				});
			}
		})
	}
}
function appendZero(obj){
	if (obj < 10) {
	  return '0' + obj 
	} else {
	  return obj
	} 
}
function getNowTimeDay(){
	var date = new Date();
	return date.getFullYear() + '-' + appendZero((date.getMonth()+1)) + '-' + appendZero(date.getDate());
}
function getNowTimeMonth(){
	var date = new Date();
	return date.getFullYear() + '-' + appendZero((date.getMonth()+1));
}
function commonMultiSelect(obj,newObj,index,name){
	if(obj[index].selFlag){
		obj[index].selFlag = false;
		for(var i=0;i<newObj.length;i++){
			if(name == newObj[i]){
				newObj.splice(i,1);
			}
		}
	}else if(obj[index].selFlag == false) {
		obj[index].selFlag = true;
		newObj.push(obj[index].name)
	}
	//console.log(newObj)
}
function replaceToBr(str){
	return str.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');
}
function dateToWeek(time){
	const weekNum = new Date(time).getDay();
	let week = '';
	switch (weekNum) {
	    case 0:
	      week = "星期日";
	      break;
	    case 1:
	      week = "星期一";
	      break;
	    case 2:
	      week = "星期二";
	      break;
	    case 3:
	      week = "星期三";
	      break;
	    case 4:
	      week = "星期四";
	      break;
	    case 5:
	      week = "星期五";
	      break;
	    case 6:
	      week = "星期六";
	      break;
	  }
	  return week;
}
export default {
	showLoading,
	hideLoading,
	showToastSuc,
	showToast,
	navigateTo,
	redirectTo,
	trim,
	patternNum,
	formatLocation,
	openMapNaviagtion,
	setClipborarData,
	comNavigateToLogin,
	getNowTimeDay,
	getNowTimeMonth,
	replaceToBr,
	dateToWeek,
	commonMultiSelect
}
